<?php
/**
 * Geneone: Content Management Framework
 * Dynamic Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ("Gene/Object/View.php");

class Gene_Object_View_Dynamic extends Gene_Object_View {
	/**
	 * Class Constructor
	 *
	 * @var object $model Gene_Object_Model object
	 */
	function Gene_Object_View_Dynamic(&$model) {
		$this->model =& $model;
	}
	
	/**
	 * Show this object. Returns HTML output code for object.
	 *
	 * @var array $actions Additional Arguments
	 *
	 * @return string HTML output for object.
	 */
	function view($actions=array()) {
		$tpl =& Gene::getTemplate();
		
		/* General Object Information */
		$info = $this->model->get();
		
		$tpl->assign ("object", $info);
		
		/* Parents */
		$parents = $this->model->getParents();
		$tpl->assign ("parents", $parents);
		
		/* Childs */
		$childs = $this->model->getChilds();
		$tpl->assign ("childs", $childs);
		
		/* Get Dynamic Page Contents */
		$filename = str_replace(array("\\", "/", "."), '', $info['content']);
		
		if (@include_once('Gene/Dynamic/'.$filename.'.php')) {
			$classname = "Gene_Dynamic_".$info['content'];
			$o =& new $classname;
			
			$o->object =& $this->model;
			
			$d = $o->call();
			
			$tpl->assign ("pagetitle", (isset($d['title']) ? $d['title'] : $this->model->info['title']));
			$tpl->assign ("content", $d['content']);
			
			if (isset($d['portlets']) && count($d['portlets'])) {
				foreach ($d['portlets'] as $i) {
					$tpl->append ("textportlets", array($i['title'], $i['content']));
				}
			}
			
			return $tpl->fetch("objects/dynamic/view");
		} else {
			return $tpl->fetch("errors/operror");
		}
	}
	
	/**
	 * Callback function to specify object specific edit actions.
	 */
	function getCustomEditActions() {
		$tpl =& Gene::getTemplate();
		
		$dynamic = array();
		
		if ($d = opendir(PEAR_GENE_PATH.'Gene/Dynamic/')) {
			while (($file = readdir($d)) !== false) {
				if ($file != "." && $file != ".." && substr($file, strlen($file)-4) == ".php") {
					$dynamic[] =  substr($file, 0, strlen($file)-4);
				}
			}
			closedir($d);
			
			$tpl->assign ("dynamicscripts", $dynamic);
		}
	}
	
	/**
	 * Displays the object creation form for this type of object.
	 *
	 * @static
	 *
	 * @var object $parent Parent Object
	 *
	 * @return string HTML output for object creation.
	 */
	function create(&$parent) {
		$tpl =& Gene::getTemplate();
		$dynamic = array();
		
		if ($d = opendir(PEAR_GENE_PATH.'Gene/Dynamic/')) {
			while (($file = readdir($d)) !== false) {
				if ($file != "." && $file != ".." && substr($file, strlen($file)-4) == ".php") {
					$dynamic[] =  substr($file, 0, strlen($file)-4);
				}
			}
			closedir($d);
			
			$tpl->assign ("dynamicscripts", $dynamic);
		}
		
		return $tpl->fetch("objects/dynamic/create");
	}
}
 
?>